# -*- coding: utf-8 -*-
import CSV_Statistics
from CSVTools import CSVData
from Rebides import RebidesDB
from BeautifulSoup import BeautifulSoup

class RebideStats:
    
    def __init__( self ):
        pass
    
    def graphStatistics( self, index, data ):
        return CSV_Statistics.graphStats( index, data )
        pass
    pass

    def CSVStatistics( self, databaseName, filename ):

        dataRows = RebidesDB().loadDatabaseRebides( databaseName )
        data = dataRows[ 0 ]
        names = dataRows[ 1 ]
        acad_years = dataRows[ 2 ]
        categorys = dataRows[ 3 ]
        grades = dataRows[ 4 ]
        
        CSV_Statistics.csvStat_1( filename, data, names, acad_years )
        CSV_Statistics.csvStat_2( filename, data, names, acad_years, categorys )
        CSV_Statistics.csvStat_3( filename, data, names, acad_years, grades )
        pass
    pass

    def HTMLStatistics( self, databaseName, filename ):

        dataRows = RebidesDB().loadDatabaseRebides( databaseName )
        data = dataRows[ 0 ]
        stabNames = dataRows[ 1 ]
        acad_years = dataRows[ 2 ]
        categorys = dataRows[ 3 ]
        grades = dataRows[ 4 ]
        
        pageHTML =  CSV_Statistics.HTMLStatistics_1( self, data, stabNames, acad_years )
        self.makeHTMLPage( filename + "_1.html", "Statistics", pageHTML )
        
        pageHTML =  CSV_Statistics.HTMLStatistics_2( self, data, stabNames, acad_years, categorys )
        self.makeHTMLPage( filename + "_2.html", "Statistics", pageHTML )
        
        pageHTML =  CSV_Statistics.HTMLStatistics_3( self, data, stabNames, acad_years, grades )
        self.makeHTMLPage( filename + "_3.html", "Statistics", pageHTML )
        pass
    pass


    def makeHTMLPage( self, name, title, bodyHTML ):
        htmlFile = file( name, "w" )

        beginHTML = "<html><head><title>{0}</title></head><body>".format( title )
        endHTML = "</body></html>"
        
        pageHTML = beginHTML + bodyHTML + endHTML
        pageHTML = BeautifulSoup( ''.join( pageHTML ) )
        pageHTML = pageHTML.prettify()
        
        htmlFile.write( pageHTML )
        htmlFile.close()
        pass
    pass

    #http://www.w3schools.com/html/html_tables.asp
    def tableHTML( self, title, data ):
        title = '''<font size="3" color="red" face="verdana"> {0} </font>'''.format( title )
        table = "<table border = 2>"
        
        for i in range(0, len ( data ) ):
            table += "<tr>"
            for j in range(0, len( data[ i ] ) ):
                table += "<td> {0} </td>".format( data[ i ][ j ] )
                pass
            table += "</tr>"
            pass
        table += "</table>"
        return title + table
    pass